import entity.Message;
import enums.ResponseType;
import request.message.MessagePullRequest;
import response.Response;
import response.message.MessagePullResponse;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class ChatView extends JFrame {
    private DefaultListModel<Message> listModel;
    private SpringLayout springLayout = new SpringLayout();
    private JPanel panel = new JPanel(springLayout);
    private JScrollPane scrollPane = new JScrollPane();
    private JButton sendButton = new JButton("发送");
    private JTextField textField = new JTextField();
    private ChatHandler chatHandler = new ChatHandler(this);

    private JList<Message> jList;

    private String hostUserName;
    private String guestUserName;

    public ChatView(String hostUserName, String guestUserName, List<Message> messageHistory) {
        super("Now chatting with:" + guestUserName);
        this.hostUserName = hostUserName;
        this.guestUserName = guestUserName;
        listModel = new DefaultListModel<>();
        for (Message message : messageHistory) {
            listModel.addElement(message);
        }
        jList = new JList<>(listModel);
        jList.setCellRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Message message = (Message) value;
                String str = message.getSenderUsername() + " 对 " + message.getReceiverUsername() + " 说: " + message.getContent();
                setText(str);
                return this;
            }
        });
        setBounds(500, 150, 800, 700);
        Font panelFont = new Font("楷体", Font.PLAIN, 18);
        sendButton.setFont(panelFont);
        sendButton.addActionListener(chatHandler);
        getRootPane().setDefaultButton(sendButton);
        textField.setFont(panelFont);
        jList.setFont(panelFont);
        textField.setPreferredSize(new Dimension(600, 32));
        scrollPane.setPreferredSize(new Dimension(781, 550));

        scrollPane.setViewportView(jList);
        panel.add(scrollPane);
        panel.add(sendButton);
        panel.add(textField);

        springLayout.putConstraint(SpringLayout.NORTH, textField, 20, SpringLayout.SOUTH, scrollPane);
        springLayout.putConstraint(SpringLayout.WEST, textField, 50, SpringLayout.WEST, scrollPane);
        springLayout.putConstraint(SpringLayout.WEST, sendButton, 20, SpringLayout.EAST, textField);
        springLayout.putConstraint(SpringLayout.NORTH, sendButton, 0, SpringLayout.NORTH, textField);

        getContentPane().add(panel);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);

        refreshChatView();
    }

    public void updateList(Message message) {
        listModel.addElement(message);
        jList.setModel(listModel);
    }

    public void updateList(List<Message> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        for (Message message : list) {
            listModel.addElement(message);
        }
        jList.setModel(listModel);
    }

    public void refreshChatView() {
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                MessagePullRequest messagePullRequest = new MessagePullRequest(guestUserName, hostUserName);
                Response response = ConnectionUtil.makeConnection(messagePullRequest);
                if (response.getType() == ResponseType.MessagePull) {
                    MessagePullResponse messagePullResponse = (MessagePullResponse) response;
                    updateList(messagePullResponse.getMessageList());
                }

            }
        }).start();
    }

    public JTextField getTextField() {
        return textField;
    }

    public String getHostUserName() {
        return hostUserName;
    }

    public String getGuestUserName() {
        return guestUserName;
    }
}
